/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.ui.controls {
	import com.proyecto404.core.EventArgs;
	import com.proyecto404.core.collections.Dictionary;
	import com.proyecto404.core.collections.IIterator;
	import com.proyecto404.core.exception.InvalidOperationException;
	import com.proyecto404.core.ui.BaseMovieClip;
	
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import org.osflash.signals.ISignal;
	import org.osflash.signals.Signal;
	import org.osflash.signals.natives.INativeDispatcher;
	import org.osflash.signals.natives.NativeSignal;
	
	/**
	 * @abstract
	 */
	public class ButtonBase extends BaseMovieClip {
		private var _enabled:Boolean;
		private var _overLastFrame:int;
		private var _outLastFrame:int;
		private var _hasDisabled:Boolean;
		
		public function ButtonBase() {
			super();
			stop();
			
			makeClickeable(this);
			_enabled = true;
			
			addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
			addEventListener(MouseEvent.ROLL_OUT, onOut, false, 0, true);
			
			if (!labels.containsKey("active")) {
				throw new InvalidOperationException("Button must contain a frame with 'active' label");
			}
			
			_hasDisabled = labels.containsKey("disabled");
			
			if (!labels.containsKey("over")) {
				throw new InvalidOperationException("Button must contain a frame with 'over' label");
			}
			
			if (labels.containsKey("pressed")) {
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
				addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 0, true);
			}
			
			calculateFrames();
		}
		
		// Calculate last frame for over and out labels
		private function calculateFrames():void {
			_overLastFrame = getLabelLastFrame("over");
			if (labels.containsKey("out")) {
				_outLastFrame = getLabelLastFrame("out");
			}
		}
		
		public override function get enabled():Boolean {
			return _enabled;
		}
		
		public function enable():void {
			var oldEnabled:Boolean = _enabled;
			_enabled = true;
			buttonMode = true;
			mouseEnabled = true;
			if (!oldEnabled) {
				onEnable();
			}
		}
		public function disable():void {
			var oldEnabled:Boolean = _enabled;
			_enabled = false;
			buttonMode = false;
			mouseEnabled = false;
			if (oldEnabled) {
				onDisable();
			}
		}
		
		protected function get overLastFrame():int {
			return _overLastFrame;
		}

		protected function get outLastFrame():int {
			return _outLastFrame;
		}

		// BEGIN OVERRIDABLE EVENTS
		
		protected function onOver(e:MouseEvent):void {}
		
		protected function onOut(e:MouseEvent):void {}
		
		protected function onMouseUp(e:MouseEvent):void {
			gotoAndStop("active");
		}
		
		protected function onMouseDown(e:MouseEvent):void {
			gotoAndStop("pressed");
		}

		protected function onEnable():void {
			gotoAndStop("active");
		}
		
		protected function onDisable():void {
			if (_hasDisabled) {
				gotoAndStop("disabled");
			}
		}
		
		// END OVERRIDABLE EVENTS 
	}
}